package com.funny.stock.trade.service;

import com.funny.stock.base.service.BaseCrudServiceV2;
import com.funny.stock.base.util.PageUtil;
import com.funny.stock.base.util.PropertiesCopyUtil;
import com.funny.stock.trade.dao.pojo.EmulateComposeDO;
import com.funny.stock.trade.dao.repo.EmulateComposeRepo;
import com.funny.stock.trade.vo.EmulateComposeVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * @Description  
 * @Author  pengl
 * @Date 2020-05-17 
 */

@Service
public class EmulateComposeService extends BaseCrudServiceV2<EmulateComposeVO,EmulateComposeDO,Long> {

	@Autowired
	EmulateComposeRepo repository;

//	@Autowired
//	ComposeAssetsService composeAssetsService;

	protected JpaRepository<EmulateComposeDO, Long> setupJpaRepository() {
		return repository;
	}

	@Transactional
	public <S extends EmulateComposeVO> S addCompose(S entity) {
		Long type = entity.getBalanceType();

		long balance = 100000;
		if(type != null){
			switch (type.intValue()){
				case 2:
					balance = 200000;
					break;
				case 3:
					balance = 500000;
					break;
				case 4:
					balance = 1000000;
					break;
				case 5:
					balance = 10000000;
					break;
			}
		}

		BigDecimal bigDecimal = BigDecimal.valueOf(balance);
		entity.setInitBalance(bigDecimal);
		entity.setCash(bigDecimal);
		entity.setIsDelete(0);
		return save(entity);
	}



	public EmulateComposeVO findByNameAndCreateId(String name,Long createId){
		return convertVO(repository.findByNameAndCreateId(name,createId));
	}

	public List<EmulateComposeVO> findMyComposeList(Long createId){
		List<EmulateComposeDO> list = repository.findByCreateIdAndIsDelete(createId, 0);
		return PropertiesCopyUtil.copyPropertiesByList(list, EmulateComposeVO.class);
	}

	public EmulateComposeVO findByCreateIdAndId(Long createId,Long id){
		return convertVO(repository.findByCreateIdAndId(createId,id));
	}

	public boolean canBuy(EmulateComposeVO composeVO, BigDecimal tradePrice, Long tradeNumber){
		BigDecimal tradeMoney = tradePrice.multiply(BigDecimal.valueOf(tradeNumber));
		return composeVO.getCash().compareTo(tradeMoney) >= 0;
	}


//	public Page<EmulateComposeVO> queryByCreateId(Long createId, Pageable pageable){
//		return repository.queryByCreateId(createId,pageable).map(this::convertVO);
//	}

	public Page<EmulateComposeVO> queryByCondition(EmulateComposeVO composeVO,Integer pageNumber,Integer pageSize){
		Specification specification = new Specification() {
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
				List<Predicate> predicates = new ArrayList<>();

				if(!StringUtils.isEmpty(composeVO.getName())) {
					predicates.add(criteriaBuilder
							.like(root.get("name"), "%" + composeVO.getName() + "%"));
				}

				if(null != composeVO.getCreateId()){
					predicates.add(criteriaBuilder.equal(root.get("createId"), composeVO.getCreateId()));
				}

				if(null != composeVO.getCreateTime()){
					predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"), composeVO.getCreateTime()));
				}

				return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		};
		PageRequest pageRequest = PageUtil.buildPageRequest(pageNumber, pageSize);
		return repository.findAll(specification,pageRequest);
	}

	@Override
	protected Class<EmulateComposeDO> getDOClass() {
		return EmulateComposeDO.class;
	}

	@Override
	protected Class<EmulateComposeVO> getVOClass() {
		return EmulateComposeVO.class;
	}

}
